/*
	       File: DirectoryProcessor.cs
	Create Date: 2007-07-31
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using GuerillaProgrammer.Components.Logging;
using GuerillaProgrammer.Components.GPThreadPool;
using GuerillaProgrammer.FileProcessor.Settings;

namespace GuerillaProgrammer.FileProcessor
{
    public class DirectoryProcessorController
    {
        #region Private member fields
        Configuration _Configuration;
        List<ConfigurationDirectory> _Directories;
        List<ConfigurationFileType> _FileTypes;
        GPThreadPool _ThreadPool;
        IWorkItemProcessor _WorkItemProcessor;
        #endregion

        #region Constructors and cleanup methods
        public DirectoryProcessorController(Configuration configuration, IWorkItemProcessor workItemProcessor)
        {
            _Configuration = configuration;
            _Directories = _Configuration.Directories;
            _FileTypes = _Configuration.FileTypes;

            int maxThreads = 1;
            int.TryParse(_Configuration.Services.MaxDirectoryWorkerThreads, out maxThreads);

            _ThreadPool = new GPThreadPool(maxThreads);
            _WorkItemProcessor = workItemProcessor;
        }
        #endregion

        #region Private implementation methods
        private void AddWorkItem(WaitCallback dirProc)
        {
            _ThreadPool.QueueItem(dirProc);
            
        }
        #endregion


        #region Public interface
        public void ProcessDirectories()
        {
            Logger.LogEnter();

            foreach (ConfigurationDirectory directory in _Directories)
            {
                Logger.LogInfo("process directory", "Processing: {0}", directory.Path);
                DirectoryProcessor dp = new DirectoryProcessor(_Configuration, directory, _FileTypes, _WorkItemProcessor);
                WaitCallback dirProc = new WaitCallback(dp.ProcessDirectory);
                _ThreadPool.QueueItem(dirProc);
            }

            Logger.LogLeave();
        }
        #endregion
    }
}
